<?php
class Price {
	private $price;
    private $db;
    public function __construct($registry) {
        $this->db = $registry->get('db');
    }

    public function createPriceJson(){
        require_once(DIR_SYSTEM . 'library/Spyc.php');
        $cache_file='system/cache/price.txt';
        $price_yaml=spyc_load_file(DIR_SYSTEM . 'library/yaml/cloud_resource_price.yaml');
        foreach($price_yaml as $region){
            foreach($region['os_type'] as $os_type){
                foreach($region[$os_type['price_list']] as $price_list){
                    $arr[$region['region']]['os_type'][$os_type['os']][$price_list['spec']]=$price_list['price'];
                }
            }
            foreach($region['vpc'] as $vpc){
                $arr[$region['region']]['vpc'][$vpc['spec']]=$vpc['price'];
            }

            foreach($region['public_ip'] as  $public_ip){
                if(isset($public_ip['base'])){
                    $spec_arr=explode('-',$public_ip['spec']);
                    $a=1;
                    for($i=$spec_arr['0'];$i<=$spec_arr['1'];$i++){
                        $arr[$region['region']]['public_ip'][$i]=(float)$public_ip['base']+($a*(float)$public_ip['incr']);
                        $a++;
                    }
                }else{
                    $arr[$region['region']]['public_ip'][$public_ip['spec']]=$public_ip['price'];
                }
            }

            foreach($region['snapshot'] as $snapshot){
                $arr[$region['region']]['snapshot'][$snapshot['spec']]=$snapshot['price'];
            }
            foreach($region['harddisk'] as $harddisk){
                $arr[$region['region']]['harddisk'][$harddisk['spec']]=$harddisk['price'];
            }
            foreach($region['image'] as $image){
                $arr[$region['region']]['image'][$image['spec']]=$image['price'];
            }

        }
        $fp = fopen($cache_file, "w");
        fwrite($fp, json_encode($arr));
        fclose($fp);
        return json_encode($arr);
    }
	public function getPrice($data) {

        $cache_file='system/cache/price.txt';
        $pice_array = json_decode(file_get_contents($cache_file),true);
        $price=0;
        if(!$pice_array){
            $pice_array=json_decode($this->createPriceJson());
        }
        if(array_key_exists('host_type',$data)){
            if(isset($pice_array[$data['region']]['os_type'][$data['os_type']])){
                $price=$pice_array[$data['region']]['os_type'][$data['os_type']][$data['host_type']];
            }else{
                $price=$pice_array[$data['region']]['os_type']['ubuntu-12.04'][$data['host_type']];
                }
        }
        if(array_key_exists('bandwidth',$data)){
            $price=$pice_array[$data['region']]['public_ip'][$data['bandwidth']];
        }
        if(array_key_exists('vpc_type',$data)){
            $price=$pice_array[$data['region']]['vpc'][$data['vpc_type']];
        }
        if(array_key_exists('hd_size',$data)){
            $price=$pice_array[$data['region']]['harddisk'][10];
        }

        return $price;
    }
//    //get hard-disk price according to different type and size
//    public function getHDPrice($region,$hd_type,$hd_size){
//    	//todo
//    	$hd_price_arr = array();
//    	$price =  0.016;
//    	if($region == "qingcloud_ap1"){
//    		$price = 0.0176;
//    	}
//    	$price_hour = round(($hd_size*$price/10)*10000)/10000;
//    	$price_month = round($price_hour*24*30*100)/100;
//    	$hd_price_arr['price_hour'] = $price_hour ;
//    	$hd_price_arr['price_month'] = $price_month ;
//    	return $hd_price_arr;
//
//
//    }

}
?>